package testdemo;
import dao.FindHouseDate;
import dao.GoodsTest;
import javabean.Goods;
import javabean.House;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import static tools.Dbutils.getSqlSession;
import static tools.Dbutils.logs;

public class TestMybatis1 {
    public static void main(String[] args) {

    }
    /**
     * 1.查询表中所有文件
     */
    @Test
    public void findAll () {
//        输出查询过程日志信息
        logs();
        SqlSession sqlSession = getSqlSession();
//        4.执行映射文件中的sql语句
        List<Object> objects = sqlSession.selectList("HouseMapper.findAllDate");
        sqlSession.commit();
        for(Object obj:objects){
            System.out.println(obj);
        }
    }

    /**
     * 2.数据插入,用insert也可以
     */
    @Test
    public void insertData(){
        logs();
        SqlSession sqlSession = getSqlSession();
        int update = sqlSession.update("HouseMapper.insert");
//        提交事务，不提交的话即使返回1但是依然没有实现数据入库
        sqlSession.commit();
        System.out.println("update = " + update);
    }

    /**
     * 3.数据更新
     */
    @Test
    public void updateDate(){
        logs();
        SqlSession sqlSession = getSqlSession();
        int update = sqlSession.update("HouseMapper.update");
        sqlSession.commit();
        System.out.println("update = " + update);
    }
    /**
     * 4.数据删除,这里用delete也行
     */
    @Test
    public void deleteDate(){
        logs();
        SqlSession sqlSession = getSqlSession();
        int update = sqlSession.update("HouseMapper.delete");
        sqlSession.commit();
        System.out.println("update = " + update);
    }

    /**
     * 5.通过指定id查询一条数据
     */
    @Test
    public void findOne(){
        logs();
        SqlSession sqlSession = getSqlSession();
        Object o = sqlSession.selectOne("HouseMapper.findOne",2);
        sqlSession.commit();
        System.out.println("o = " + o);
    }
//    动态设置多个参数值
    /**
     * 6.动态给定属性值插入一条数据
     */
    @Test
    public void insert1(){
        logs();
        SqlSession sqlSession = getSqlSession();
//        准备数据
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("name","笑笑");
        objectObjectHashMap.put("tel","789");
        objectObjectHashMap.put("address","上海");
        objectObjectHashMap.put("rent","5000");
        objectObjectHashMap.put("state","1");
        int update = sqlSession.update("HouseMapper.insert1", objectObjectHashMap);
        sqlSession.commit();
        System.out.println("update = " + update);
    }

    /**
     * 7.删除,这里用delete 这个api是可以的
     */
    @Test
    public void delete1(){
        logs();
        SqlSession sqlSession = getSqlSession();
        int delete = sqlSession.delete("HouseMapper.delete1",5);
        sqlSession.commit();
        System.out.println("delete = " + delete);
    }

    /**
     * 8.更改一条数据中的多个属性
     */
    @Test
    public void update1(){
        logs();
        SqlSession sqlSession = getSqlSession();
        House house = new House();
        house.setId(4);
        house.setTel("655");
        house.setRent("5000");
        house.setState("1");
        int update = sqlSession.update("HouseMapper.update1", house);
        sqlSession.commit();
        System.out.println("update = " + update);
    }
    /**
     * 9.查找多个字段的值，这里一定要注意查找的属性写成${cols}
     */
    @Test
    public void select1(){
        logs();
        SqlSession sqlSession = getSqlSession();
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("cols","name,tel,address");
        List<Object> objects = sqlSession.selectList("HouseMapper.select1", objectObjectHashMap);
        for(Object obj:objects){
            System.out.println(obj);
        }
    }
//    2025/3/26
    /**
     * 10.模糊查询：查找名字中有小的人的信息
     */
    @Test
    public void selectName(){
        logs();
        SqlSession sqlSession = getSqlSession();
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("name","%小%");
        List<Object> objects = sqlSession.selectList("HouseMapper.selectName", objectObjectHashMap);
        sqlSession.commit();
        for(Object obj : objects){
            System.out.println(obj);
        }
    }

    /**
     * 11.条件查询：查询租金在某一区间内的信息
     */
    @Test
    public void selectRent(){
        logs();
        SqlSession sqlSession = getSqlSession();
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
//        可能将 String 类型按照字符编码规则进行比较，而不是按照数值大小比较,由于表中rent时varchar，所以在传入整型虽然会自动转换，但也可能因为类型不匹配而引发错误
        /*objectObjectHashMap.put("minRent","200");
        objectObjectHashMap.put("maxRent","5000");*/
        objectObjectHashMap.put("minRent",200);
        objectObjectHashMap.put("maxRent",5000);
        List<Object> objects = sqlSession.selectList("HouseMapper.selectRent", objectObjectHashMap);
        sqlSession.commit();
        for(Object obj : objects){
            System.out.println(obj);
        }
    }

    /**
     * 12.批量删除：一次删除多个id
     */
    @Test
    public void deleteByIds(){
        logs();
        SqlSession sqlSession = getSqlSession();
        int []arr={2,11};
        int delete = sqlSession.delete("HouseMapper.deleteByIds", arr);
        sqlSession.commit();
        System.out.println("delete = " + delete);
    }

//    第二种获取mapper的方式，接口代理,查询不用提交事务？
    @Test
    public void findByIdTest(){
        logs();
        SqlSession sqlSession = getSqlSession();
        FindHouseDate mapper = sqlSession.getMapper(FindHouseDate.class);
        House byId = mapper.findById(13);
        System.out.println("byId = " + byId);
    }

    @Test
    public void findAllTest(){
        logs();
        SqlSession sqlSession = getSqlSession();
        FindHouseDate mapper = sqlSession.getMapper(FindHouseDate.class);
        List<House> all = mapper.findAll();
        for(House s : all){
            System.out.println(s);
        }
    }

    @Test
    public void insert2(){
        logs();
        SqlSession sqlSession = getSqlSession();
        GoodsTest mapper = sqlSession.getMapper(GoodsTest.class);
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("name","灯具");
        objectObjectHashMap.put("goods_num",20);
        int i = mapper.insert2(objectObjectHashMap);
        sqlSession.commit();
        System.out.println("i = " + i);
    }

    @Test
    public void delete2(){
        logs();
        SqlSession sqlSession = getSqlSession();
        GoodsTest mapper = sqlSession.getMapper(GoodsTest.class);
        int i = mapper.delete2(11);
        sqlSession.commit();
        System.out.println("i = " + i);
    }
    @Test
    public void update2(){
        logs();
        SqlSession sqlSession = getSqlSession();
        GoodsTest mapper = sqlSession.getMapper(GoodsTest.class);
        int i = mapper.update2();
        sqlSession.commit();
        System.out.println("i = " + i);
    }
    @Test
    public void selectAll2(){
        logs();
        SqlSession sqlSession = getSqlSession();
        GoodsTest mapper = sqlSession.getMapper(GoodsTest.class);
        List<Goods> goods = mapper.selectAll2();
        for(Goods g : goods){
            System.out.println(g);
        }
    }
}
